package com.parking.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.StateData;
import com.parking.platform.dao.PlatformAccountMapper;
import com.parking.platform.dao.PlatformAccountRoleMapper;
import com.parking.platform.dao.PlatformRoleMapper;
import com.parking.platform.dto.PlatformRoleDto;
import com.parking.platform.dto.RoleNameDto;
import com.parking.platform.entity.PlatformAccount;
import com.parking.platform.entity.PlatformAccountRole;
import com.parking.platform.entity.PlatformRole;
import com.parking.platform.exception.PlatformException;
import com.parking.platform.exception.PlatformExceptionCode;
import com.parking.platform.form.SelRoleForm;
import com.parking.platform.service.PlatformRoleService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@Service
public class PlatformRoleServiceImpl extends ServiceImpl<PlatformRoleMapper, PlatformRole> implements PlatformRoleService {
    @Autowired
    private PlatformRoleMapper platformRoleMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Resource
    private PlatformAccountMapper platformAccountMapper;
    @Resource
    private PlatformAccountRoleMapper platformAccountRoleMapper;
    /**
     * 新增角色接口
     * 参数：
     * 角色名称：roleName
     *
     * */
    @Override
    @Transactional
    public void addPlatformRole(String roleName,Integer adminId) {
        QueryWrapper<PlatformRole> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_role_name",roleName);
        PlatformRole platformRole = platformRoleMapper.selectOne(wrapper);
        if (platformRole!=null) {
            throw new PlatformException(PlatformExceptionCode.LOGIN_FAILED);
        }
        PlatformRole role = new PlatformRole();
        role.setPlatformRoleName(roleName);
        role.setPlatformRoleState(StateData.PLATFORM_ROLE_STATE_ENABLE);
        platformRoleMapper.insert(role);
        role.setPlatformRolePerson(String.valueOf(adminId));
        role.setPlatformRoleTime(LocalDateTime.now());
        HashMap<Object, Object> map = new HashMap<>();
        map.put("platformRole",role);
        System.out.println(map);
        rabbitTemplate.convertAndSend("addPlatformRole",map);

    }
    /**
     * 删除用户
     * 参数
     * 令牌：authorization
     * 账号状态：state
     * */
    @Override
    @Transactional
    public void deleteRoleState(Integer platformRoleId, Integer adminId) {
        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
        QueryWrapper<PlatformAccountRole> queryWrapper = wrapper.eq("platform_account_id", adminId);
        PlatformAccountRole accountRole = platformAccountRoleMapper.selectOne(queryWrapper);
        if (accountRole.getPlatformRoleId()==platformRoleId) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_MY);
        }
        PlatformRole platformRole = platformRoleMapper.selectById(platformRoleId);
        if (platformRole==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_NO_EXIST);
        }
        if (platformRole.getPlatformRoleState()== StateData.PLATFORM_ROLE_STATE_DELETE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_STATE_DELETE);
        }
        QueryWrapper<PlatformAccountRole> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("platform_role_id",platformRoleId);
        List<PlatformAccountRole> platformAccountRoles = platformAccountRoleMapper.selectList(wrapper1);
        List<Integer> list = platformAccountRoles.stream().map(PlatformAccountRole::getPlatformAccountId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(list)) {
            List<PlatformAccount> platformAccounts = platformAccountMapper.selectBatchIds(list);
            for (PlatformAccount platformAccount : platformAccounts) {
                platformAccount.setPlatformState(StateData.PLATFORM_ROLE_STATE_DISABLE);
                platformAccountMapper.updateById(platformAccount);
            }
        }
        platformRole.setPlatformRoleState(StateData.PLATFORM_ROLE_STATE_DELETE);
        platformRoleMapper.updateById(platformRole);

    }
    /**
     * 禁用角色状态
     * 参数
     * 令牌：authorization
     * 账号状态：state
     * */
    @Override
    @Transactional
    public void downRoleState(Integer platformRoleId, Integer adminId) {
        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
        QueryWrapper<PlatformAccountRole> queryWrapper = wrapper.eq("platform_account_id", adminId);
        PlatformAccountRole accountRole = platformAccountRoleMapper.selectOne(queryWrapper);
        if (accountRole.getPlatformRoleId()==platformRoleId) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_MY);
        }
        PlatformRole platformRole = platformRoleMapper.selectById(platformRoleId);
        if (platformRole==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_NO_EXIST);
        }
        if (platformRole.getPlatformRoleState()== StateData.PLATFORM_ROLE_STATE_DISABLE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_STATE_DOWN);
        }
        if (platformRole.getPlatformRoleState()== StateData.PLATFORM_ROLE_STATE_DELETE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_STATE_DELETE);
        }
        QueryWrapper<PlatformAccountRole> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("platform_role_id",platformRoleId);
        List<PlatformAccountRole> platformAccountRoles = platformAccountRoleMapper.selectList(wrapper1);
        List<Integer> list = platformAccountRoles.stream().map(PlatformAccountRole::getPlatformAccountId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(list)) {
            List<PlatformAccount> platformAccounts = platformAccountMapper.selectBatchIds(list);
            for (PlatformAccount platformAccount : platformAccounts) {
                platformAccount.setPlatformState(StateData.PLATFORM_ROLE_STATE_DISABLE);
                platformAccountMapper.updateById(platformAccount);
            }
        }
        platformRole.setPlatformRoleState(StateData.PLATFORM_ROLE_STATE_DISABLE);
        platformRoleMapper.updateById(platformRole);
    }
    /**
     * 启用角色状态
     * 参数
     * 令牌：authorization
     * 账号状态：state
     * */
    @Override
    @Transactional
    public void enableRoleState(Integer platformRoleId, Integer adminId) {
        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
        QueryWrapper<PlatformAccountRole> queryWrapper = wrapper.eq("platform_account_id", adminId);
        PlatformAccountRole accountRole = platformAccountRoleMapper.selectOne(queryWrapper);
        if (accountRole.getPlatformRoleId()==platformRoleId) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_MY);
        }
        PlatformRole platformRole = platformRoleMapper.selectById(platformRoleId);
        if (platformRole==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_NO_EXIST);
        }
        if (platformRole.getPlatformRoleState()== StateData.PLATFORM_ROLE_STATE_ENABLE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_STATE_ENABLE);
        }
        if (platformRole.getPlatformRoleState()== StateData.PLATFORM_ROLE_STATE_DELETE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_STATE_DELETE);
        }
        platformRole.setPlatformRoleState(StateData.PLATFORM_ROLE_STATE_ENABLE);
        platformRoleMapper.updateById(platformRole);
    }
    /**
     * 修改角色接口
     * 参数：
     * 角色名称：roleName
     * 角色ID：platformRoleId
     *
     * */
    @Override
    @Transactional
    public void upPlatformRole(String roleName, Integer platformRoleId, Integer adminId) {
        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
        QueryWrapper<PlatformAccountRole> queryWrapper = wrapper.eq("platform_account_id", adminId);
        PlatformAccountRole accountRole = platformAccountRoleMapper.selectOne(queryWrapper);
        if (accountRole.getPlatformRoleId()==platformRoleId) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_MY);
        }
        PlatformRole platformRole = platformRoleMapper.selectById(platformRoleId);
        if (platformRole==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_NO);
        }
        platformRole.setPlatformRoleName(roleName);
        platformRoleMapper.updateById(platformRole);
    }
    /**
     * 查询全部角色
     *  参数
     *   角色名：platformRoleName
     *  pageNum:当前页数
     *  pageSize:当前条数
     *  state:状态
     * 返回 PlatformRoleDto
     * */
    @Override
    public PageInfo findRoleAll(SelRoleForm form) {
        Page<PlatformRole> page = new Page<>(form.getPageNum()!=null?form.getPageNum():1, form.getPageSize()!=null?form.getPageSize():6);
        QueryWrapper<PlatformRole> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(form.getPlatformRoleName())) {
            wrapper.like("platform_role_name",form.getPlatformRoleName());
        }
        if(form.getState()!=null&&form.getState()!=-1){
            wrapper.eq("platform_role_state",form.getState());
        }
        platformRoleMapper.selectPage(page,wrapper);
        List<PlatformRoleDto> dtos = BeanUtil.copyToList(page.getRecords(), PlatformRoleDto.class);
        for (PlatformRoleDto dto : dtos) {
            if (ObjectUtil.isNotEmpty(dto.getPlatformRolePerson())) {
                dto.setPlatformRolePersonName(platformAccountMapper.selectById(dto.getPlatformRolePerson()).getPlatformAccount());
            }
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(dtos);
        return pageInfo;
    }
    /**
     * 查询全部角色
     *  无参数
     * 返回 RoleNameDto
     * */
    @Override
    public List<RoleNameDto> findRoleName() {
        QueryWrapper<PlatformRole> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_role_state",StateData.PLATFORM_ROLE_STATE_ENABLE);
        List<PlatformRole> platformRoles = platformRoleMapper.selectList(wrapper);
        return BeanUtil.copyToList(platformRoles,RoleNameDto.class);

    }
    /**
     * 最高管理员
     * 修改角色接口
     * 参数：
     * 角色名称：roleName
     *
     * */
    @Override
    public void upPlatformPower(Integer power, Integer platformRoleId,Integer adminId) {
//        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
//        wrapper.eq("platform_account_id",adminId);
//        PlatformAccountRole accountRole = platformAccountRoleMapper.selectOne(wrapper);
//        if (accountRole.getPlatformRoleId()==accountRole.getPlatformRoleId()) {
//
//        }
        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
        QueryWrapper<PlatformAccountRole> queryWrapper = wrapper.eq("platform_account_id", adminId);
        PlatformAccountRole accountRole = platformAccountRoleMapper.selectOne(queryWrapper);
        if (accountRole.getPlatformRoleId()==platformRoleId) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_MY);
        }
        PlatformRole platformRole = platformRoleMapper.selectById(platformRoleId);
        if (platformRole==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_NO);
        }
        platformRole.setPlatformPower(power);
        platformRoleMapper.updateById(platformRole);
    }
}
